package blockqueue;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue; 

/**
 * LinkedBlockingQueue测试
 */
public class BlockingQueueTest {

	public static void main(String[] args) throws InterruptedException {
		// 声明一个容量为10的LinkedBlockingQueue
		BlockingQueue<String> linkedBlockingQueue = new LinkedBlockingQueue<String>(10);
		
		// 创建了3个生产者和1个消费者
		Producer producer1 = new Producer(linkedBlockingQueue);
		Producer producer2 = new Producer(linkedBlockingQueue);
		Producer producer3 = new Producer(linkedBlockingQueue);
		Consumer consumer = new Consumer(linkedBlockingQueue);
		
		// 借助Executors线程池，启动生产者和消费者
		ExecutorService service = Executors.newCachedThreadPool();
		service.execute(producer1);
//		service.execute(producer2);
//		service.execute(producer3);
		service.execute(consumer);
		
		// 执行10s
		Thread.sleep(10 * 1000);
		producer1.stop();
//		producer2.stop();
//		producer3.stop();

		Thread.sleep(2000);
		// 退出Executor
		service.shutdown();
		
	}
	
}
